﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SheepCommon.API.SheepCommands.Breedingsettinges;

namespace SheepCommon.API.SheepCommands.PregnancyTestes
{
    public class PregnancyTestQueryHandler : IRequestHandler<PregnancyTestQueryCommand, ResultPage<PregnancyTestQueryDto>>
    {
        private readonly IBaseRepository<PregnancyTest> _pregnancyTestRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IMapper _mapper;

        public PregnancyTestQueryHandler(IBaseRepository<PregnancyTest> pregnancyTestRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IMapper mapper)
        {
            _pregnancyTestRepository = pregnancyTestRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _mapper = mapper;
        }


        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<PregnancyTestQueryDto>> Handle(PregnancyTestQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<PregnancyTestQueryDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var pregnancyTest = await (from a in await _pregnancyTestRepository.GetAll()
                                           join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                           join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                           where a.IsDeleted == false
                                           select new PregnancyTestQueryDto()
                                           {
                                               PregnancyTestId = a.PregnancyTestId,
                                               PregnancyTestDate = a.PregnancyTestDate,
                                               EarBugles = a.EarBugles,
                                               PregnancyTestResults = a.PregnancyTestResults,
                                               PregnancyTestWays = a.PregnancyTestWays,
                                               NumberParities = a.NumberParities,
                                               PregnancyDays = a.PregnancyDays,
                                               CurrentBuildings = a.CurrentBuildings,
                                               CurrentColumns = a.CurrentColumns,
                                               CottageId = b.CottageId,
                                               CottageName = b.CottageName,
                                               FieldId = c.FieldId,
                                               FieldName = c.FieldName,
                                               Responsible = a.Responsible,
                                               DocumentNo = a.DocumentNo,
                                               CreatedTime = a.CreatedTime,
                                               CreatedOwnerId = a.CreatedOwnerId,
                                           }).ToListAsync();
                if (request.StartPregnancyTestDate != null)
                {
                    pregnancyTest = pregnancyTest.Where(c => c.PregnancyTestDate >= request.StartPregnancyTestDate).ToList();
                }
                if (request.EndPregnancyTestDate != null)
                {
                    pregnancyTest = pregnancyTest.Where(c => c.PregnancyTestDate <= request.EndPregnancyTestDate).ToList();
                }
                result.TotalCount = pregnancyTest.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                pregnancyTest = pregnancyTest.OrderBy(c => c.PregnancyTestId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = pregnancyTest;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
